<?php

namespace app\api\controller;

use app\common\controller\Api;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function getsource(){
        exit;
        $list = Db::name('goods_stock_log')->select();
        $type = dill_type();
        foreach ($list as $v){
            foreach ($type as $key=> $val){
                if ($v['type'] == $val){
                    Db::name('goods_stock_log')->where(array('id'=>$v['id']))->update(array('source'=>$key));
                }
            }


        }
    }
    public function edita(){
        exit;
        $list = Db::name('analysis_collection')->select();
        foreach ($list as $v){
            if($v['is_income'] == 1 ){
                $row = Db::name('customer')->where(array('company'=>$v['settle_name']))->find();
                Db::name('analysis_collection')->where(array('id'=>$v['id']))->update(array('obj_id'=>$row['id']));
                Db::name('analysis_log')->where(array('analysis_collection_id'=>$v['id']))->update(array('obj_id'=>$row['id']));
            }else{
                $row = Db::name('supplier')->where(array('nickname'=>$v['settle_name']))->find();
                Db::name('analysis_collection')->where(array('id'=>$v['id']))->update(array('obj_id'=>$row['id']));
                Db::name('analysis_log')->where(array('analysis_collection_id'=>$v['id']))->update(array('obj_id'=>$row['id']));
            }
        }
    }
    public function editgoods(){
        return false;
        $list = Db::name('sales_goods')
            ->where(array('sfhj'=>0))
            ->select();
        foreach ($list as $v){
            Db::name('sales_goods')->where(array('id'=>$v['id']))->update(array('sfhj'=>$v['ht_price']*$v['goods_num']));
        }
    }

    public function editlog(){
        exit;
        $list = Db::name('analysis_log')->where(array('analysis_collection_id'=>0,'is_income'=>array('<',3)))->select();
        Db::startTrans();
        try {
            foreach ($list as $v) {
                $start_time = strtotime(date('y-m-d', $v['createtime']));
//                dump($start_time);
                $where['createtime'] = array('between time', [$start_time, $start_time + 86400]);
                $num = Db::name('analysis_collection')->where($where)->count();
                $cdata = array(
                    'number' => 'XS-T-' . date('Y-m-d', $v['createtime']) . '-' . getnumber($num),
                    'paymoney' => $v['price'],
                    'image' => $v['image'],
                    'remark' => $v['remark'],
                    'is_income' => $v['is_income'],
                    'admin_id' => $v['admin_id'],
                    'group_id' => $v['group_id'],
                    'pay_time' => $v['pay_time'],
                    'make_time' => $v['make_time'],
                    'company' => $v['company'],
                    'settle_name' => $v['settle_name'],
                    'operator' => $v['operator'],
                    'storehouse_id' => $v['storehouse_id'],
                    'pay_id' => $v['pay_id'],
                    'discount' => 0,
                    'createtime' => $v['createtime'],
                );
                $collection_id =  Db::name('analysis_collection')->insertGetId($cdata);
                if(empty($collection_id)){
                    throw new Exception('联系管理员');
                }
                $arr = explode(',', $v['analysis_id']);
                $data = [];

                foreach ($arr as $value) {
                    $tdata = $v;
                    $tdata['analysis_id'] = $value;
                    $tdata['analysis_collection_id'] = $collection_id;
                    $arow = Db::name('analysis')->where(array('id'=>$value))->find();
                    if(count($arr)>1 && $v['price'] > $arow['cost']){
                        $tdata['price'] = $arow['cost'];
                    }
                    $data[] = $tdata;
                }
//                dump($data);
                Db::name('analysis_log')->where(array('id'=>$v['id']))->delete();
                foreach ($data as $k=> $v){
                    unset($data[$k]['id']);
                }
                Db::name('analysis_log')->insertAll($data);
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }
    public function bugbug(){

        
//        $start_time = 1606269828;
//        $end_time = 1607326503;
//        $where['createtime'] = array('between time',[$start_time,$end_time]);
//        $where['out_num'] = array('>',0);
//        $list = Db::name('goods_stock_log')->where($where)->select();
//        foreach ($list as $v){
//            $data = array(
//              'balance'=>$v['now_num']*$v['final_cost'],
//            );
//            Db::name('goods_stock_log')->where(array('id'=>$v['id']))->update($data);
//        }
//        exit;





//        $where['createtime'] = array('between time',[$start_time,$end_time]);
//        $where['type'] = '销售出库单';
//        $list = Db::name('goods_stock_log')->where($where)->select();
////        dump($list);
//        foreach ($list as $v){
//            $data = array(
//              'before_num'=>$v['now_num'],
//              'now_num'=>$v['now_num']-$v['out_num'],
//            );
//            Db::name('goods_stock_log')->where(array('id'=>$v['id']))->update($data);
//        }
    }



}
